package com.block.job.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.LocalDateTimeUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.common.service.LogFactory;
import com.block.db.entity.ToolsNewsInfo;
import com.block.db.mapper.MerchantThirdBaseInfoMapper;
import com.block.db.mapper.MerchantThirdProtocolInfoMapper;
import com.block.db.mapper.SourceMemberDataInfoHistroyMapper;
import com.block.db.mapper.ToolsNewsInfoMapper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import org.slf4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Component
public class ToolPullNewsJob {

    private static final Logger log = LogFactory.getJobLog();

    @Resource
    ToolsNewsInfoMapper toolsNewsInfoMapper;

    /**
     * 每半个小时拉取一次新闻
     * @throws Exception
     */
    @XxlJob("toolPullNewsJob")
    public void toolPullNewsJob() throws Exception {
        XxlJobHelper.log("数据清理---------开始执行--------");

        pullNews();

        XxlJobHelper.log("数据清理 ---------结束执行--------");
    }

    public void pullNews(){

        String apiKey = "e80d52009f28b81fbc8ceb002e020fe4";
        String apiUrl = "http://apis.juhe.cn/fapigx/caijing/query?key={}&num={}";

        String requestUrl = StrUtil.format(apiUrl, apiKey, 20);
        String result = HttpUtil.createGet(requestUrl).execute().body();

        JSONObject resultObject = JSON.parseObject(result);

        Integer error_code = resultObject.getInteger("error_code");
        if (!ObjectUtil.equals(error_code, Integer.valueOf(0))){
            throw new RuntimeException("拉取新闻消息异常");
        }

        // 填充新闻消息
        JSONObject result1 = resultObject.getJSONObject("result");
        JSONArray newsList = result1.getJSONArray("newslist");

        fillNewsInfo(newsList);
    }

    public void fillNewsInfo(JSONArray newsList){

        for (int i = 0; i < newsList.size(); i++) {
            JSONObject jsonObject = newsList.getJSONObject(i);

            String id = jsonObject.getString("id");
            String ctime = jsonObject.getString("ctime");
            String title = jsonObject.getString("title");
            String description = jsonObject.getString("description");
            String source = jsonObject.getString("source");
            String picUrl = jsonObject.getString("picUrl");
            String url = jsonObject.getString("url");

            ToolsNewsInfo newsInfo = new ToolsNewsInfo();
            newsInfo.setNewsId(id);
            newsInfo.setTitleInfo(title);
            newsInfo.setDescInfo(description);
            newsInfo.setAuthor(source);

            if (!picUrl.startsWith("https::")){
                picUrl = "https:" + picUrl;
            }

            newsInfo.setPicUrl(picUrl);

            if (!url.startsWith("https::")){
                url = "https:" + url;
            }
            newsInfo.setContentUrl(url);

            LocalDateTime parse = LocalDateTimeUtil.parse(ctime, DatePattern.NORM_DATETIME_MINUTE_PATTERN);
            newsInfo.setPublishTime(parse);

            try {
                toolsNewsInfoMapper.insert(newsInfo);
            }catch (Exception exception){
                exception.printStackTrace();
            }
        }

    }


}
